define(['jquery', 'bootstrap', 'backend','frontend', 'form', 'template','highcharts'], function ($, undefined,Backend, Frontend, Form, Template, Highcharts) {
    var Controller = {
        info: function () {
            Controller.basedlink();
            Controller.filedownload();
            Controller.webpage();
            Controller.video();
            Controller.game();
            //页面要求5分钟刷新一次
            setInterval(function(){
                Controller.basedlink();
                Controller.filedownload();
                Controller.webpage();
                Controller.video();
                Controller.game();
            }, 300000);              
        },
        basedlink: function () {
            // console.log(456);
            $.ajax({
                url: '/index/quality/basedlink',
                type:'post',
                data: {propertyname: 'basedlink'},
                dataType:'json',
                success:function(json){  
                    //console.log(json)                  
                    Controller.piechart(json, "qualityScore", "basedlink");                    
                }
            });
        },
        filedownload: function () {
            // console.log(456);
            $.ajax({
                url: '/index/quality/filedownload',
                type:'post',
                data: {propertyname: 'filedownload'},
                dataType:'json',
                success:function(json){
                     console.log(json);                    
                    Controller.piechart(json, "qualityScore", "filedownload");                    
                }
            });
        },
        webpage: function () {
            // console.log(456);
            $.ajax({
                url: '/index/quality/webpage',
                type:'post',
                data: {propertyname: 'webpage'},
                dataType:'json',
                success:function(json){
                    
                    Controller.piechart(json, "qualityScore", "webpage");                    
                }
            });
        },
        video: function() {
            //console.log(456);
            $.ajax({
                url: '/index/quality/video',
                type:'post',
                data: {propertyname: 'video'},
                dataType:'json',
                success:function(json){
                    
                    Controller.piechart(json, "qualityScore", "video");                    
                }
            });
        },
        game: function() {
            //console.log(456);
            $.ajax({
                url: '/index/quality/game',
                type:'post',
                data: {propertyname: 'game'},
                dataType:'json',
                success:function(json){
                    
                    Controller.piechart(json, "qualityScore", "game");                    
                }
            });
        },
        piechart: function(json, type, id){ //画图 饼图/趋势图   数据   类型(画图的字段后期可扩展)  id
            var toolTipValue="";
            var text = "";
            if(id == 'webpage' ){
                text = '网页测试';
            }else if(id == 'filedownload'){
                text = '下载速率';
            }else if(id == 'basedlink'){
                text = '基础链路';
            }else if(id == 'video'){
                text = '视频';
            }else if(id == 'game'){
                text = '游戏';
            }

            if (type == "firstFramesTime") {
                text += '首帧时长(平均值)指标趋势图';
                toolTipValue = "首帧时长";
            } else if (type == "firstVideoTime") {
                text += '总缓冲时长(平均值)指标趋势图';
                toolTipValue = "总缓冲时长";
            } else if(type == "downloadSpeed"){
                text += '下载速率(平均值)指标趋势图';
                toolTipValue = "下载速率";
            } else if(type == "totalBufferNumber"){
                text += '总缓冲次数(平均值)指标趋势图';
                toolTipValue = "总缓冲次数";
            } else if(type == "blankScreen"){
                text += '黑屏(平均值)指标趋势图';
                toolTipValue = "黑屏";
            } else if(type == "qualityScore"){
                text += '质量分趋势图';
                toolTipValue = "质量分";
            }

            if(json.code == 401){
                Backend.api.toastr.error(json.msg);
                window.location.href='/index/user/login';
                return false;
            }

            if(json.code == 0 || json.code == 403){
                // alert(msg);
                options = {
                    positionClass: "toast-top-center",
                };
                Backend.api.toastr.error(text+json.msg,'',options);
                return false;
            }
            // console.log(json.xAxis);
            // console.log(json.series);
            Highcharts.chart(id, {
                chart: {
                    // zoomType: 'x',
                    // type: 'area'
                },
                title: {
                    style: {
                        // color: 'red',   //字体颜色
                        fontSize:"14px", //字体大小
                        "font-weight": "bold",
                    },
                    text: text,
                    style: {
                        // color: 'red',   //字体颜色
                        fontSize:"14px", //字体大小
                        "font-weight": "bold",
                    },
                    x: -20 //center
                },

                credits: {
                    enabled: false
                },           
                xAxis: {
                    tickInterval:10,//刻度的宽度,
                    categories : json.xAxis,
                    labels:{
                        x:45
                    },
                },
                yAxis: {
                    title: {
                        text: null,
                    },
                    labels: {
                        formatter : function(){//设置纵坐标的样式
                            if(type=="001"&&id=="blankScreen"){
                                if(this.value=="0.00"){
                                    return "否";
                                }else{
                                    return "是";
                                }
                            }else{
                                return this.value;
                            }
                        }
                    },
                    plotLines: [{
                        value: 0,
                        width: 1,
                        color: '#808080'
                    }],
                    // min:0,
                    // tickInterval:20,//刻度的宽度             
                },
                legend: {
                    // enabled: false   //隐藏图例
                },
                tooltip : {
                    formatter : function() {
                        if(type=="001"&&id=="blankScreen"){
                            if(this.point.y=="0.00"){
                                return '<b>' + this.series.name + '</b><br>时间:' + this.key + '<br>'+toolTipValue+'：' + "否";
                            }else{
                                return '<b>' + this.series.name + '</b><br>时间:' + this.key + '<br>'+toolTipValue+'：' + "是";
                            }
                        }
                        else
                            return '<b>' + this.series.name + '</b><br>时间:' + this.key + '<br>'+toolTipValue+'：' + this.point.y;
                    }
                },                    
                // series:[{
                //     seriesshowInLegend: false,   //隐藏数据名称
                //     type :'area',
                //     // name:'网信',
                //     data:json.series
                // }]
                series:json.series
            });            
        },
    };
    return Controller;
});